package com.ydd.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.dto.SysConfigDTO;
import com.ydd.entity.SysConfig;
import com.ydd.mapper.SysConfigMapper;
import com.ydd.result.Result;
import com.ydd.service.SysConfigService;
import com.ydd.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangxiaojun
 * @since 2023-05-14
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    @Autowired(required = false)
    SysConfigMapper sysConfigMapper;

    /**
     * 获取系统配置
     *
     * @return
     */
    @Override
    public Result getConfig() {

        QueryWrapper<SysConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tab_value","system");
        List<SysConfig> sysConfigList = sysConfigMapper.selectList(queryWrapper);

        Map<String,String> map = new HashMap<>();

        for (SysConfig sysConfig : sysConfigList) {
            String attrKey = sysConfig.getAttrKey();
            String attrValue = sysConfig.getAttrValue();

            map.put(attrKey,attrValue);
        }


        return Result.success(map);
    }





    //所有配置
    @Override
    public SysConfigVO findAll() {
        //创建sysConfigVO对象
        SysConfigVO sysConfigVO = new SysConfigVO();
        //设置allData
        List<SysConfig> sysConfigs = sysConfigMapper.findAll();
        List<AllDataVO> allData = new ArrayList<>();
        for (SysConfig sysConfig : sysConfigs) {
            AllDataVO allDataVO = new AllDataVO();
            BeanUtils.copyProperties(sysConfig, allDataVO);
            allData.add(allDataVO);
        }
        sysConfigVO.setAllData(allData.toArray());
        //设置configFormVO
        ConfigFormVO configFormVO = ConfigFormVO.builder()
                .blackIp(sysConfigMapper.findAttrValueByAttrKey("black_ip"))
                .closeWebsite(sysConfigMapper.findAttrValueByAttrKey("close_website"))
                .copyCode(sysConfigMapper.findAttrValueByAttrKey("copy_code"))
                .copyInfo(sysConfigMapper.findAttrValueByAttrKey("copy_info"))
                .copyUrl(sysConfigMapper.findAttrValueByAttrKey("copy_url"))
                .loginBg(sysConfigMapper.findAttrValueByAttrKey("login_bg"))
                .loginTitle(sysConfigMapper.findAttrValueByAttrKey("login_title"))
                .loginregBg(sysConfigMapper.findAttrValueByAttrKey("loginreg_bg"))
                .noticContent(sysConfigMapper.findAttrValueByAttrKey("notic_content"))
                .noticTitle(sysConfigMapper.findAttrValueByAttrKey("notic_title"))
                .phone(sysConfigMapper.findAttrValueByAttrKey("phone"))
                .siteKeywords(sysConfigMapper.findAttrValueByAttrKey("site_keywords"))
                .siteLink(sysConfigMapper.findAttrValueByAttrKey("site_link"))
                .siteName(sysConfigMapper.findAttrValueByAttrKey("site_name"))
                .systemLogo(sysConfigMapper.findAttrValueByAttrKey("system_logo"))
                .systemName(sysConfigMapper.findAttrValueByAttrKey("system_name"))
                .webIco(sysConfigMapper.findAttrValueByAttrKey("web_ico"))
                .webImg(sysConfigMapper.findAttrValueByAttrKey("web_img"))
                .build();
        sysConfigVO.setConfigForm(configFormVO);
        //设置configRules
        ConfigRules site_name = new ConfigRules(sysConfigMapper.findAttrValueByAttrKey("site_name"));
        sysConfigVO.setConfigRules(site_name);
        //设置list
        List<ListVO> listVOList = new ArrayList<>();
        List<SysConfig> sysConfigList = sysConfigMapper.findAll();
        for (SysConfig sysConfig : sysConfigList) {
            //遍历listVo,查看是否有重复的key
            if (listVOList != null && listVOList.size() > 0) {
                //count为0时则表示没重复key,可添加对象
                int count = listVOList.size();
                for (ListVO list : listVOList) {
                    if (!list.getKey().equals(sysConfig.getTabValue())) {
                        count--;
                        continue;
                    }
                    //若有相同key则结束
                    if (list.getKey().equals(sysConfig.getTabValue())) {
                        break;
                    }
                }
                //无重复key时则添加
                if (count == 0) {
                    ListVO listVO = new ListVO();
                    //封装key，name
                    listVO.setKey(sysConfig.getTabValue());
                    String tabValue = sysConfig.getTabValue();
                    if (tabValue.equals("custom")) {
                        listVO.setName("其他设置");
                    } else if (tabValue.equals("base")) {
                        listVO.setName("基础配置");
                    } else {
                        listVO.setName("系统配置");
                    }
                    listVOList.add(listVO);
                }
                //第一次添加
            } else {
                ListVO listVO = new ListVO();
                //封装key，name
                listVO.setKey(sysConfig.getTabValue());
                String tabValue = sysConfig.getTabValue();
                if (tabValue.equals("custom")) {
                    listVO.setName("其他设置");
                } else if (tabValue.equals("base")) {
                    listVO.setName("基础配置");
                } else {
                    listVO.setName("系统配置");
                }
                listVOList.add(listVO);
            }
        }
        //遍历list
        for (ListVO listVO : listVOList) {
            //目标：封装children
            List<ChildrenVO> childrenVOList = new ArrayList<>();
            //遍历sysConfigList 找key相同的封装
            for (SysConfig sysConfig : sysConfigList) {
                if (sysConfig.getTabValue().equals(listVO.getKey())) {
                    //封装data
                    ChildrenVO childrenVO = new ChildrenVO();
                    List<Object> dataList = new ArrayList<>();
                    //遍历遍历sysConfigList，找group相同的装到dataList，最后datalist转data数组
                    for (SysConfig config : sysConfigList) {
                        if (sysConfig.getGroupValue().equals(config.getGroupValue())) {
                            dataList.add(config);
                        }
                    }
                    childrenVO.setData(dataList.toArray());
                    childrenVO.setName(sysConfig.getGroupValue());
                    //去重
                    int countChild = childrenVOList.size();
                    for (ChildrenVO child : childrenVOList) {
                        if (child.getName().equals(childrenVO.getName())) {
                            break;
                        }
                        if (!child.getName().equals(childrenVO.getName())) {
                            countChild--;
                        }
                    }
                    if (countChild == 0) {
                        childrenVOList.add(childrenVO);
                    }
                }
            }
            listVO.setChildren(childrenVOList.toArray());
        }
        sysConfigVO.setList(listVOList.toArray());

        return sysConfigVO;
    }


    //添加配置
    @Override
    public void add(SysConfigDTO dto) {
        SysConfig sysConfig = new SysConfig();
        BeanUtils.copyProperties(dto, sysConfig);
        sysConfig.setDelFlag(1);
        sysConfigMapper.add(sysConfig);
    }
}
